(function ($) {
	$.fn.enableBt = function () {
        $(this).attr('disabled', false);
        if ($.browser.msie && $.browser.version < 9) {
            $(this).removeClass('disabled');
        }
    }
    $.fn.disableBt = function () {
        $(this).attr('disabled', true);
        if ($.browser.msie && $.browser.version < 9) {
            $(this).addClass('disabled');
        }
    }	
	$.fn.expand = function (duration, callback) {
        this.each(function (i) {
            var element = $(this);
            element.css('display', 'block');
            element.stop(true).css({
                'overflow': '',
                'height': '',
                'paddingTop': '',
                'paddingBottom': '',
                'borderTopWidth': '',
                'borderBottomWidth': '',
                'marginTop': '',
                'marginBottom': ''
            });
            var height = element.height();
            var paddingTop = parseInt(element.css('padding-top'));
            var paddingBottom = parseInt(element.css('padding-bottom'));
            var marginTop = parseInt(element.css('margin-top'));
            var marginBottom = parseInt(element.css('margin-bottom'));
            var css = {
                'overflow': 'hidden',
                'height': 0,
                'paddingTop': 0,
                'paddingBottom': 0
            };
            var anim = {
                'height': height,
                'paddingTop': paddingTop,
                'paddingBottom': paddingBottom
            };
            if (!$.browser.msie || $.browser.version > 8) {
                var borderTopWidth = parseInt(element.css('border-top-width'));
                var borderBottomWidth = parseInt(element.css('border-bottom-width'));
                css.borderTopWidth = '1px';
                css.borderBottomWidth = '1px';
                anim.borderTopWidth = borderTopWidth;
                anim.borderBottomWidth = borderBottomWidth;
            }
            var prev = element.prev();
            if (prev.length === 0 && parseInt(element.css('margin-bottom')) + marginTop !== 0) {
                css.marginTop = Math.min(0, marginTop);
                css.marginBottom = Math.min(0, marginBottom);
                anim.marginTop = marginTop;
                anim.marginBottom = marginBottom;
            }
            element.stop(true).css(css).animate(anim, {
                'duration': duration,
                'complete': function () {
                    $(this).css({
                        'display': '',
                        'overflow': '',
                        'height': '',
                        'paddingTop': '',
                        'paddingBottom': '',
                        'borderTopWidth': '',
                        'borderBottomWidth': '',
                        'marginTop': '',
                        'marginBottom': ''
                    });
                    if (callback) {
                        callback.apply(this);
                    }
                    if ($.browser.msie && $.browser.version < 8) {
                        $(this).css('zoom', 1);
                    }
                }
            });
        });
        return this;
    };	
	$.fn.foldAndRemove = function (duration, callback) {
        $(this).fold(duration, function () {
            if (callback) {
                callback.apply(this);
            }
            $(this).remove();
        });
        return this;
    }	
	$.fn.fold = function (duration, callback) {
        this.each(function (i) {
            var element = $(this);
            var marginTop = parseInt(element.css('margin-top'));
            var marginBottom = parseInt(element.css('margin-bottom'));
            var anim = {
                'height': 0,
                'paddingTop': 0,
                'paddingBottom': 0
            };
            if (!$.browser.msie || $.browser.version > 8) {
                anim.borderTopWidth = '1px';
                anim.borderBottomWidth = '1px';
            }
            var prev = element.prev();
            if (prev.length === 0 && parseInt(element.css('margin-bottom')) + marginTop !== 0) {
                anim.marginTop = Math.min(0, marginTop);
                anim.marginBottom = Math.min(0, marginBottom);
            }
            element.stop(true).css({
                'overflow': 'hidden'
            }).animate(anim, {
                'duration': duration,
                'complete': function () {
                    $(this).css({
                        'display': 'none',
                        'overflow': '',
                        'height': '',
                        'paddingTop': '',
                        'paddingBottom': '',
                        'borderTopWidth': '',
                        'borderBottomWidth': '',
                        'marginTop': '',
                        'marginBottom': ''
                    });
                    if (callback) {
                        callback.apply(this);
                    }
                }
            });
        });
        return this;
    };
	$.fn.removeBlockMessages = function (options) {
        var settings = $.extend({}, $.fn.removeBlockMessages.defaults, options);
        this.each(function (i) {
            var block = $(this);
            if (!block.hasClass('block-content')) {
                block = block.find('.block-content:first');
                if (block.length == 0) {
                    block = $(this).closest('.block-content');
                    if (block.length == 0) {
                        return;
                    }
                }
            }
            var messages = block.find('.message');
            if (settings.only) {
                if (typeof settings.only == 'string') {
                    settings.only = [settings.only];
                }
                messages.filter('.' + settings.only.join(', .'));
            } else if (settings.except) {
                if (typeof settings.except == 'string') {
                    settings.except = [settings.except];
                }
                messages.not('.' + settings.except.join(', .'));
            }
            if (settings.animate) {
                messages.foldAndRemove();
            } else {
                messages.remove();
            }
        });
        return this;
    };
    $.fn.removeBlockMessages.defaults = {
        only: false,
        except: false,
        animate: true
    };	
	$.fn.blockMessage = function (message, options) {
        var settings = $.extend({}, $.fn.blockMessage.defaults, options);
        this.each(function (i) {
            var block = $(this);
            if (!block.hasClass('block-content')) {
                block = block.find('.block-content:first');
                if (block.length == 0) {
                    block = $(this).closest('.block-content');
                    if (block.length == 0) {
                        return;
                    }
                }
            }
            var messageClass = (settings.type == 'info') ? 'message' : 'message ' + settings.type;
            if (settings.noMargin) {
                messageClass += ' no-margin';
            }
            var finalMessage = (typeof message == 'object') ? '<ul class="' + messageClass + '"><li>' + message.join('</li><li>') + '</li></ul>' : '<p class="' + messageClass + '">' + message + '</p>';
            if (settings.position == 'top') {
                var children = block.find('h1, .h1, .block-controls, .block-header, .wizard-steps');
                if (children.length > 0) {
                    var lastHeader = children.last();
                    var next = lastHeader.next('.message');
                    while (next.length > 0) {
                        lastHeader = next;
                        next = lastHeader.next('.message');
                    }
                    var messageElement = lastHeader.after(finalMessage).next();
                } else {
                    var messageElement = block.prepend(finalMessage).children(':first');
                }
            } else {
                var children = block.find('.block-footer:last-child');
                if (children.length > 0) {
                    var messageElement = children.before(finalMessage).prev();
                } else {
                    var messageElement = block.append(finalMessage).children(':last');
                }
            }
            if (settings.animate) {
                messageElement.expand();
            }
        });
        return this;
    };
    $.fn.blockMessage.defaults = {
        type: 'info',
        position: 'top',
        animate: true,
        noMargin: true
    };	
})(jQuery);